package com.betterjr.modules.risk.loan.service;

import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import com.betterjr.common.service.BaseService;
import com.betterjr.common.utils.Collections3;
import com.betterjr.common.utils.QueryTermBuilder;
import com.betterjr.modules.risk.loan.dao.LoanProjectBankMapper;
import com.betterjr.modules.risk.loan.data.LoanProjectBankConstantCollections;
import com.betterjr.modules.risk.loan.data.LoanProjectRelationConstantCollections;
import com.betterjr.modules.risk.loan.data.LoanTransactionRelationConstantCollections;
import com.betterjr.modules.risk.loan.entity.LoanProjectBank;
import com.betterjr.modules.risk.loan.entity.LoanProjectRelation;
import com.google.common.collect.Lists;

@Service
public class LoanProjectBankService extends BaseService<LoanProjectBankMapper, LoanProjectBank> {

    /**
     * 
     * @Title: queryBanks 
     * @Description:查询银行列表
     * @param anLoanId
     * @param anType
     * @return 
     * @throws 
     * @author xuyp
     * @date 2018年6月27日 下午6:08:07
     */
    @SuppressWarnings("unchecked")
    public List<LoanProjectBank> queryBanks(Long anLoanId, String anType) {

        if (anLoanId != null && anLoanId != 0L) {

            Map<String, Object> map = QueryTermBuilder.newInstance()
                    .put("businStatus", LoanTransactionRelationConstantCollections.RELATIONS_BUSINSTATUS_OK)
                    .put("loanId", anLoanId).build();

            if (StringUtils.isNoneBlank(anType)) {
                map.put("type", anType.split(LoanProjectRelationConstantCollections.FILE_DATA_SEPATORY_CHAR));
            }

            return this.selectByProperty(map, "id Desc");

        }

        return Lists.newArrayList();
    }

    /**
     * 
     * @Title: saveOrUpdateBanksByLoan 
     * @Description: 更新银行表记录
     * @param anLoan
     * @return TODO(说明)
     * @throws 
     * @author xuyp
     * @date 2018年6月27日 下午6:37:56
     */
    public List<LoanProjectBank> saveOrUpdateBanksByLoan(LoanProjectRelation anLoan) {

        List<LoanProjectBank> lists = Lists.newArrayList();

        if (anLoan != null && anLoan.getId() != null && anLoan.getId() != 0L) {

            if (anLoan.getCustGatheringBank() != null) lists.add(saveOrUpdateBanks(anLoan.getCustGatheringBank()));

            if (anLoan.getCustPayServiceBank() != null) lists.add(saveOrUpdateBanks(anLoan.getCustPayServiceBank()));

            if (anLoan.getCustWithHoldPrincipalBank() != null)
                lists.add(saveOrUpdateBanks(anLoan.getCustWithHoldPrincipalBank()));

            if (anLoan.getCustWithHoldInterestBank() != null)
                lists.add(saveOrUpdateBanks(anLoan.getCustWithHoldInterestBank()));

            if (anLoan.getFactoryLoanBank() != null) lists.add(saveOrUpdateBanks(anLoan.getFactoryLoanBank()));

            if (anLoan.getFactoryGatheringPrincipalBank() != null)
                lists.add(saveOrUpdateBanks(anLoan.getFactoryGatheringPrincipalBank()));

            if (anLoan.getFactoryGatheringInterestBank() != null)
                lists.add(saveOrUpdateBanks(anLoan.getFactoryGatheringInterestBank()));

            // if (anLoan.getCustPayServiceBank() != null) lists.add(saveOrUpdateBanks(anLoan.getCustPayServiceBank()));

            saveOrUpdateBanks(anLoan.getId(), anLoan.getCustAccountRegulatoryBank());

            saveOrUpdateBanks(anLoan.getId(), anLoan.getCustAggregationBank());

        }

        return lists;

    }

    /**
     * 
     * @Title: saveOrUpdateBanks 
     * @Description: 更新银行表记录
     * @param anLoanId
     * @param anBanks
     * @return TODO(说明)
     * @throws 
     * @author xuyp
     * @date 2018年6月27日 下午6:37:06
     */
    public List<LoanProjectBank> saveOrUpdateBanks(Long anLoanId, List<LoanProjectBank> anBanks) {

        if (!Collections3.isEmpty(anBanks)) {

            for (LoanProjectBank bank : queryBanks(anLoanId, Collections3.getFirst(anBanks).getType())) {
                bank.setBusinStatus(LoanTransactionRelationConstantCollections.RELATIONS_BUSINSTATUS_DELETED);
                this.updateByPrimaryKeySelective(bank);
            }

            for (LoanProjectBank bank : anBanks) {
                if (bank.getId() == null || bank.getId() == 0L) bank.saveAddInitValue(bank.getType());

                if (bank.getLoanId() == null || bank.getLoanId() == 0L || bank.getLoanId() != anLoanId)
                    bank.setLoanId(anLoanId);

                this.insertSelective(bank);
            }

        }

        return anBanks;

    }

    /**
     * 
     * @Title: saveOrUpdateBanks 
     * @Description:新增单条类型的银行数据
     * @param anBank
     * @return TODO(说明)
     * @throws 
     * @author xuyp
     * @date 2018年6月27日 下午6:30:10
     */
    public LoanProjectBank saveOrUpdateBanks(LoanProjectBank anBank) {

        if (anBank != null && anBank.getLoanId() != null && StringUtils.isNoneBlank(anBank.getType())) {

            List<LoanProjectBank> list = this.queryBanks(anBank.getLoanId(), anBank.getType());

            if (!Collections3.isEmpty(list)) {

                for (LoanProjectBank loanProjectBank : list) {
                    loanProjectBank
                            .setBusinStatus(LoanTransactionRelationConstantCollections.RELATIONS_BUSINSTATUS_DELETED);
                    this.updateByPrimaryKeySelective(loanProjectBank);
                }

            }

            if (anBank.getId() == null) anBank.saveAddInitValue(anBank.getType());

            this.insertSelective(anBank);

        }

        return anBank;

    }

    /**
     * 
     * @Title: saveUnfriendLoan 
     * @Description: 解除和贷款项目的绑定关系
     * @param anLoan
     * @return TODO(说明)
     * @throws 
     * @author xuyp
     * @date 2018年6月27日 下午6:51:38
     */
    public List<LoanProjectBank> saveUnfriendLoan(Long anLoan) {

        if (anLoan != null && anLoan != null) {

            List<LoanProjectBank> banks = queryBanks(anLoan, "");
            for (LoanProjectBank bank : banks) {
                bank.setBusinStatus(LoanTransactionRelationConstantCollections.RELATIONS_BUSINSTATUS_DELETED);
                this.updateByPrimaryKeySelective(bank);
            }
            return banks;
        }

        return Lists.newArrayList();
    }

}
